package com.jiaoyou.baojian.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.agora.AgoraHelper;
import com.jiaoyou.baojian.entity.CallResponse;
import com.jiaoyou.baojian.entity.InviteExtra;
import com.jiaoyou.baojian.entity.UserInfo;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.otto.Event;
import com.jiaoyou.baojian.utils.Utils;
import com.squareup.otto.Subscribe;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

import static io.agora.rtc.Constants.CHANNEL_PROFILE_COMMUNICATION;

public class CalloutActivity extends BaseActivity {

    @BindView(R.id.header)
    ImageView header;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.calloff)
    ImageView calloff;

    private UserInfo userInfo;
    private String avatar, nickname;

    private String channelID;
    private String channelKey;

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);

        needOtto();

        userInfo = (UserInfo) getIntent().getExtras().getSerializable("userInfo");
        userInfo.imageList = null;
        avatar = getIntent().getExtras().getString("avatar");
        nickname = getIntent().getExtras().getString("nickname");

        Utils.showCircleImage(this, avatar, header);
        name.setText(nickname);

//        if(getLoginResponse().user != null)
//            channelID = Utils.getP2PChannelName(getLoginResponse().user.id, userInfo.userId);
//        else{
//            finish();
//            return;
//        }
        Api.getInstance().roomCallv1(userInfo.userId + "", new BaofangCallback<CallResponse>() {
            @Override
            public void onFailure(Call call, Throwable t) {
                toast("呼叫失败：" + t.getLocalizedMessage());
                finish();
            }

            @Override
            public void onSuccess(CallResponse callResponse) {
                channelID = callResponse.agoraChannelName;
                channelKey = callResponse.agoraChannelKey;
                InviteExtra inviteExtra = new InviteExtra();
                inviteExtra.userInfo = getLoginResponse().userInfo;
                AgoraHelper.getInstance().agoraAPI.channelInviteUser2(channelID, userInfo.userId + "",
                        getGson().toJson(inviteExtra));
            }
        });

        mediaPlayer = MediaPlayer.create(this, R.raw.waiting);
        mediaPlayer.setLooping(true);
        mediaPlayer.setVolume(100, 100);
        mediaPlayer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(mediaPlayer != null){
            mediaPlayer.stop();
        }
    }

    @Override
    public int getContentView() {
        return R.layout.activity_callout;
    }

    @OnClick(R.id.calloff)
    public void onViewClicked() {
        toast("取消");
        AgoraHelper.getInstance().agoraAPI.channelInviteEnd(channelID, userInfo.userId + "", 0);
        finish();
    }

    @Subscribe
    public void onEvent(String event){
        if(event.equals(Event.AGORA_INVITE_REFUSED_BY_PEER)){
            toastFullscreen("被对方拒绝");
            finish();
        } else if(event.equals(Event.AGORA_INVITE_E_OTHER)){
            toastFullscreen("呼叫失败：未知错误");
            finish();
        } else if(event.equals(Event.AGORA_INVITE_E_NET)){
            toastFullscreen("呼叫失败：网络错误");
            finish();
        } else if(event.equals(Event.AGORA_INVITE_E_PEER_OFFLINE)){
            toastFullscreen("呼叫失败：对方不在线");
            finish();
        } else if(event.equals(Event.AGORA_INVITE_E_REINVITE)){
            toastFullscreen("呼叫失败：不要重复呼叫");
            finish();
        } else if(event.equals(Event.AGORA_INVITE_E_TIMEOUT)){
            toastFullscreen("长时间无应答，请稍后再拨");
            finish();
        } else if(event.equals(Event.AGORA_INVITE_ACCEPTED_BY_PEER)){
            Intent intent1 = new Intent(this, TalkingActivity.class);
            intent1.putExtra("actress", false);
            intent1.putExtra("channel_name", channelID);
            intent1.putExtra("channel_key", channelKey);
            intent1.putExtra("userInfo", userInfo);
            startActivity(intent1);
            finish();
        }
    }
}
